package com.bto.solar.work.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.bto.solar.work.common.BizException;
import com.bto.solar.work.constant.BizCode;
import com.bto.solar.work.entity.Company;
import com.bto.solar.work.entity.Department;
import com.bto.solar.work.mapper.CompanyMapper;
import com.bto.solar.work.mapper.DepartmentMapper;
import com.bto.solar.work.dao.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bto.solar.work.utils.StringUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author adrian
 * @since 2023-08-09
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
//
//    @Autowired
//    private CompanyMapper companyMapper;
//
//    @Autowired
//    private DepartmentMapper departmentMapper;
//
//    @Override
//    public List<Company> getDepartmentAll() {
//        //获取所有部门 department
//        List<Department> departments = departmentMapper.selectList(null);
//        ArrayList<Department> list = new ArrayList<>();
//        HashMap<String, Department> map = new HashMap<>();
//        //把所有部门转化成map结构，key为组织编码，value为组织对象
//        for (Department department : departments) {
//            if (StringUtils.isNotEmpty(department.getDepartmentId().toString())){
//                map.put(department.getDepartmentId().toString() , department);
//            }
//        }
//        //实现树形结构
//        for (Department department : departments) {
//            //获取父级id
//            String code = department.getParentId().toString();
//            Department parent = map.get(code);
//            //根据最高级组织中parentCode为0或者以parentCode为组织编码查不到组织的原理，把最高级组织对象add到list结构中
//            if (department.getParentId() == 0){
//                list.add(department);
//            }else {
//                Collection<Department> children = parent.getDepartments();
//                if (CollectionUtils.isEmpty(children)){
//                    parent.setDepartments(new ArrayList<>());
//                }
//                parent.getDepartments().add(department);
//            }
//        }
//
//        //创建map来存放公司id，和部门集合
//        Map<Long, List<Department>> Map = new HashMap<>();
//        Map = list.stream().collect(Collectors.groupingBy(Department::getCompanyId));
//
//        List<Company> companies = companyMapper.selectList(null);
//        Map<Long, List<Department>> finalMap = Map;
//        companies.stream().forEach(company -> {
//            Long companyId = company.getCompanyId();
//            company.setDepartments(finalMap.get(companyId));
//        });
//     //   return companies;
//        return null;
//    }
//
//
//    @Override
//    public Integer addDepartment(Department department) {
//        if (department.getDepartmentId() != null){
//            throw new BizException(BizCode.BizCommonExceptionCode);
//        }
//        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Company::getCompanyId , department.getCompanyId());
//        if (companyMapper.selectOne(wrapper) == null){
//            throw new BizException(BizCode.NOT_HAVE_COMPANY_ERROR);
//        }
//        Date date = new Date(System.currentTimeMillis());
//        long time = date.getTime();
//        department.setCreateTime(new Timestamp(time));
//        department.setUpdateTime(new Timestamp(time));
//        Integer insert = departmentMapper.insert(department);
//        return insert;
//        return null;
//    }
//
//    @Override
//    public Integer updateDepartment(Department department , Integer id) {
//        Date date = new Date(System.currentTimeMillis());
//        long time = date.getTime();
//        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Department::getDepartmentId , id);
//        Department selectOne = departmentMapper.selectOne(wrapper);
//        if (selectOne == null){
//            throw new BizException(BizCode.NOT_HAVE_ROLE_ERROR);
//        }
//        department.setUpdateTime(new Timestamp(time));
//        Integer update = departmentMapper.update(department, wrapper);
//        return update;
//        return null;
//    }
//
//    @Override
//    public Integer deleteDepartment(Integer id) {
////        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
////        wrapper.eq(Department::getDepartmentId , id);
////        Department selectOne = departmentMapper.selectOne(wrapper);
////        if (selectOne == null){
////            throw new BizException(BizCode.NOT_HAVE_ROLE_ERROR);
////        }
////        Integer delete = departmentMapper.delete(wrapper);
////        return delete;
//        return null;
//    }
}
